package com._520it._03_many2one_one2many.test;

import com._520it._03_many2one_one2many.domain.Department;
import com._520it._03_many2one_one2many.domain.Employee;
import com._520it._03_many2one_one2many.mapper.DepartmentMapper;
import com._520it._03_many2one_one2many.mapper.EmployeeMapper;
import com._520it.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


public class App {
    //=====================many2one=============================

    @Test
    public void testSave() throws Exception {

        //many2one
        //先创建对象,设置属性
        Employee employee1 = new Employee();
        employee1.setName("张飞");
        Employee employee2 = new Employee();
        employee2.setName("李鹏");

        Department department = new Department();
        department.setName("行政部");

        //设置关系
        employee1.setDept(department);
        employee2.setDept(department);

        //保存
        //先保存one方
        SqlSession session = MybatisUtil.getSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);

        departmentMapper.save(department);
        employeeMapper.save(employee1);
        employeeMapper.save(employee2);
        session.commit();
        session.close();
    }


    //查询
    //额外SQL的方式
    @Test
    public void testGet() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Employee employee = employeeMapper.get(2L);
        System.out.println(employee);
        session.close();
    }

    @Test
    public void testGet1() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Employee employee = employeeMapper.get1(2L);
        System.out.println(employee);
        session.close();
    }


    @Test
    public void testList() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        List<Employee> employeeList = employeeMapper.list();
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
        session.close();
    }


    //单向many2one: 删除外键所关联的飚中的数据
    @Test
    public void testDelete() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);

        //先删除外键关联数据, 在删除所关联的表中的数据
        employeeMapper.updateRelation(7L);

        //删除外键关联关系后, 再删除外键所关联的飚中的数据
        departmentMapper.delete(7L);

        session.commit();
        session.close();
    }

    //=====================one2many=============================


    @Test
    public void testSave2() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        //创建部门,设置属性
        Department department = new Department();
        department.setName("技术公关部");

        Employee employee1 = new Employee();
        employee1.setName("蒋泽明");
        Employee employee2 = new Employee();
        employee2.setName("胡景涛");

        //设置关系
        List<Employee> list = new ArrayList<>();
        list.add(employee1);
        list.add(employee2);

        //保存
        departmentMapper.save(department);
        employeeMapper.save(employee1);
        employeeMapper.save(employee2);

        // 更新关系-----部门要根据每个员工的id 更新与每个员工的关系.因此采用循环  不更新关系: 外键列的值为null
        for (Employee employee : list) {
            departmentMapper.updateRelation(department.getId(), employee.getId());
        }

        session.commit();
        session.close();

    }


    @Test
    public void testGet2() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);

        Department department = departmentMapper.get(8L);
        List<Employee> list = department.getEmployeelist();

        System.out.println(department.getName());
        for (Employee employee : list) {
            System.out.println(employee);
        }
        session.close();
    }


    //显式的外连接方式
    @Test
    public void testGet3() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);

        Department department = departmentMapper.get1(8L);
        System.out.println(department);
        session.close();
    }


    //通过部门查询员工
    @Test
    public void testList2() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        DepartmentMapper departmentMapper = session.getMapper(DepartmentMapper.class);
        List<Department> list = departmentMapper.list();
        for (Department department : list) {
            System.out.println(department.getEmployeelist());
        }
        session.close();
    }


}
